﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace BIN
{
    public class Common
    {
        public static string CombineDir(params string[] Para)
        {
            return System.IO.Path.Combine(Para);
        }
        public static void CheckFolderExists(string Folder)
        {
            if (!(System.IO.Directory.Exists(Folder))) System.IO.Directory.CreateDirectory(Folder);
        }
        public static string GetApplicationBaseDir()
        {
            string appDir = AppDomain.CurrentDomain.BaseDirectory;
            if (!appDir.EndsWith("\\")) appDir += "\\";
            return appDir;
        }
        public static bool FileExists(string FileName)
        {
            return (System.IO.File.Exists(FileName));
        }
        public readonly static string AppDir = GetApplicationBaseDir();
        public static void CheckDownUrl(ref string Url, ref string ToDir)
        {
            if (Url.StartsWith("\\\\"))
                Url = Url.Replace('/', '\\');
            else
                Url = Url.Replace('\\', '/');
            ToDir = ToDir.Replace('/', '\\');
        }
        /// <summary>
        /// 取去除文件扩展名之后的文件字符串
        /// </summary>
        /// <param name="Prog"></param>
        /// <returns></returns>
        private static string GetShortExec(string Prog)
        {
            int Pos = Prog.LastIndexOf(".");
            return (Pos > 0 ? Prog.Substring(0, Pos) : Prog);
        }
        /// <summary>
        /// Prog是一个不带路径或后缀.EXE或.COM的文件名,如notepad
        /// </summary>
        /// <param name="Prog"></param>
        /// <param name="Kill"></param>
        /// <returns></returns>
        public static bool ProgRunned(string Prog, bool Kill)
        {
            string prog = GetShortExec(Prog);
            System.Diagnostics.Process[] mProcs =
                System.Diagnostics.Process.GetProcessesByName(prog);
            foreach (System.Diagnostics.Process sProc in mProcs)
            {
                if (string.Compare(sProc.ProcessName, prog, true) == 0)
                {
                    if (Kill) sProc.Kill(); //殺死進程
                    return true;
                }
            }
            return false;
        }
        public static void BeginProcessArgs(string ProgName, string Args, string WorkDir, bool HideMe = false)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            try
            {
                p.StartInfo.FileName = ProgName;
                if (!string.IsNullOrEmpty(WorkDir)) p.StartInfo.WorkingDirectory = WorkDir;
                if (!string.IsNullOrEmpty( Args)) p.StartInfo.Arguments = Args;
                p.StartInfo.UseShellExecute = true;
                if (HideMe)
                {
                    p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                }
                p.Start();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void BeginProcessArgs(string ProgName, string Args, string WorkDir)
        {
            BeginProcessArgs(ProgName, Args, WorkDir, true);
        }
    }
}
